﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using DevComponents.AdvTree;

#pragma warning disable 0162

namespace Custom_Recommended
{
    public partial class frmImport : Form
    {
        Dictionary<string, bool> Imports = new Dictionary<string, bool>();

        public frmImport()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Application.StartupPath;
            ofd.Multiselect = false;
            ofd.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
            ofd.FilterIndex = 0;

            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlNodeList xmlTranslations;
                XmlNodeList xmlNList;
                txtPath.Text = ofd.FileName;
                Imports = new Dictionary<string, bool>();

                try
                {
                    xmlDoc.Load(ofd.FileName);
                }
                catch (XmlException)
                {
                    return;
                }

                treeHeroes.Nodes.Clear();
                xmlNList = xmlDoc.SelectNodes("/crsettings/heroes/hero");
                if (xmlNList.Count == 0) xmlNList = xmlDoc.SelectNodes("/heroes/hero");
                if (xmlNList.Count == 0)
                {
                    MessageBox.Show("Selected file is not a valid Custom Recommended file!");
                    lblVersion.Text = "Version:";
                    return;
                }

                xmlDoc.Save(ofd.FileName);
                lblVersion.Text = "Version: " + xmlDoc.ChildNodes[0].Attributes["version"].Value;

                xmlDoc.Load(Application.StartupPath + "\\crsettings.xml");
                xmlTranslations = xmlDoc.SelectNodes("/crsettings/items/item");
                xmlDoc.Save(Application.StartupPath + "\\crsettings.xml");

                foreach (XmlNode hero in xmlNList)
                {
                    Node node = new Node();
                    node.Text = hero.Attributes["name"].Value;
                    node.CheckBoxVisible = true;

                    if (hero.Attributes["enabled"].Value == "true")
                    {
                        node.Cells.Add(new Cell("True"));
                        Imports.Add(hero.Attributes["name"].Value, true);
                    }
                    else
                    {
                        node.Cells.Add(new Cell("False"));
                        Imports.Add(hero.Attributes["name"].Value, false);
                    }

                    foreach (XmlNode item in hero.ChildNodes)
                    {
                        if (item.InnerText == "Empty_Item")
                        {
                            Node node2 = new Node();
                            node2.Text = "";
                            node.Nodes.Add(node2);
                            node2.Dispose();
                        }
                        else
                        {
                            foreach (XmlNode translation in xmlTranslations)
                            {
                                if (translation.Attributes["internal"].Value == item.InnerText)
                                {
                                    Node node2 = new Node();
                                    node2.Text = translation.Attributes["name"].Value;
                                    node.Nodes.Add(node2);
                                    node2.Dispose();
                                }
                            }
                        }
                    }

                    treeHeroes.Nodes.Add(node);
                    node.Dispose();
                }

                treeHeroes.Columns[0].Sort();
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (Node hero in treeHeroes.Nodes)
            {
                hero.Checked = true;
            }
        }

        private void btnSelectNone_Click(object sender, EventArgs e)
        {
            foreach (Node hero in treeHeroes.Nodes)
            {
                hero.Checked = false;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtPath.Text != "")
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlNodeList xmlImport;
                XmlNodeList xmlLocal;
                xmlDoc.Load(txtPath.Text);

                xmlImport = xmlDoc.SelectNodes("/crsettings/heroes/hero");
                if (xmlImport.Count == 0) xmlImport = xmlDoc.SelectNodes("/heroes/hero");
                if (xmlImport.Count == 0) return;
                xmlDoc.Save(txtPath.Text);

                xmlDoc.Load(Application.StartupPath + "\\crsettings.xml");
                xmlLocal = xmlDoc.SelectNodes("/crsettings/heroes/hero");

                foreach (XmlNode hero in xmlImport)
                {
                    foreach (Node node in treeHeroes.Nodes)
                    {
                        if (node.Text == hero.Attributes["name"].Value && node.Checked)
                        {
                            foreach (XmlNode local in xmlLocal)
                            {
                                if (local.Attributes["name"].Value == hero.Attributes["name"].Value)
                                {
                                    local.Attributes["enabled"].Value = hero.Attributes["enabled"].Value;

                                    for (int count = 0; count <= 19; count++)
                                    {
                                        if (hero.ChildNodes[count] != null)
                                        {
                                            local.ChildNodes[count].InnerText = hero.ChildNodes[count].InnerText;
                                        }
                                        else
                                        {
                                            local.ChildNodes[count].InnerText = "Empty_Item";
                                        }
                                    }

                                    continue;
                                }
                                else if (local.Attributes["name"].Value == "The " + hero.Attributes["name"].Value)
                                {
                                    local.Attributes["enabled"].Value = hero.Attributes["enabled"].Value;

                                    for (int count = 0; count <= 19; count++)
                                    {
                                        if (hero.ChildNodes[count] != null)
                                        {
                                            local.ChildNodes[count].InnerText = hero.ChildNodes[count].InnerText;
                                        }
                                        else
                                        {
                                            local.ChildNodes[count].InnerText = "Empty_Item";
                                        }
                                    }

                                    continue;
                                }
                                else
                                {
                                    continue;
                                }

                                break;
                            }

                            break;
                        }
                    }
                }

                xmlDoc.Save(Application.StartupPath + "\\crsettings.xml");
                MessageBox.Show("Import successful!");
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
